package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginDTO;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveDTO;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import cn.tedu.ivos.user.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author liyuehui
 * @time 2024/7/4 9:35
 */
@Api(tags = "用户模块")
@RestController
@Slf4j
@RequestMapping("/v1/user/")
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @return:JsonResult
     */
    @PostMapping("login")
    @ApiOperation("用户登录")
    public JsonResult login(@RequestBody @Validated UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session) {
        log.debug("用户登录:" + userLoginDTO);
        UserVO userVO = userService.login(userLoginDTO);
        session.setAttribute("user", userVO);
        log.debug("登录成功:" + userVO);
        return JsonResult.ok(userVO);
    }

    /**
     * 新建用户
     *
     * @return:JsonResult
     */
    @PostMapping("save")
    @ApiOperation("保存用户")
    public JsonResult save(UserSaveDTO userSaveDTO) {
        log.debug("保存用户:" + userSaveDTO);
        userService.save(userSaveDTO);
        log.debug("保存成功");
        return JsonResult.ok();
    }

    /**
     * 查询全部用户
     * @return: userList
     */
    @GetMapping("select")
    @ApiOperation("查询用户")
    public JsonResult selectByUsername(UserQuery userQuery) {
        log.debug("查询用户:" + userQuery);
        List<User> userList = userService.selectUser(userQuery);
        log.debug("成功查询到" + userList.size() + "个用户");
        return JsonResult.ok(userList);
    }

    /**
     * 重置用户密码
     * @return:JsonResult
     */
    @PostMapping("update/password/{userId}")
    @ApiOperation("重置用户密码")
    public JsonResult resetPassword(@PathVariable Long userId) {
        log.debug("重置密码用户id:" + userId);
        userService.resetPassword(userId);
        log.debug("重置成功");
        return JsonResult.ok();
    }

    /**
     * 修改用户状态
     * @return:JsonResult
     */
    @PostMapping("update/status/{userId}/{status}")
    @ApiOperation("修改用户状态")
    public JsonResult updateStatus(@PathVariable Long userId, @PathVariable String status) {
        log.debug("请求修改用户状态:" + userId + "修改成" + status);
        userService.updateStatus(userId, status);
        log.debug("修改成功");
        return JsonResult.ok();
    }

    /**
     * 删除用户
     * @return:JsonResult
     */
    @DeleteMapping("delete/{userId}")
    @ApiOperation("删除用户")
    public JsonResult delete(@PathVariable Long userId) {
        log.debug("请求删除用户:" + userId);
        userService.delete(userId);
        log.debug("删除成功");
        return JsonResult.ok();
    }

    /**
     * 查询用户待审批列表
     * @return JsonResult
     */
    @GetMapping("select/audit/{parentId}")
    @ApiOperation("查询用户待审批列表")
    public JsonResult selectAudit(@PathVariable Long parentId) {
        log.debug("查询用户待审批列表:" + parentId);
        List<User> userList = userService.selectAudit(parentId);
        log.debug("成功查询到" + userList.size() + "个用户");
        return JsonResult.ok(userList);
    }
}
